package cn.edu.lingnan.tourism.controller;

import cn.edu.lingnan.tourism.exeption.TourismException;
import cn.edu.lingnan.tourism.pojo.Route;
import cn.edu.lingnan.tourism.service.RouteService;
import cn.edu.lingnan.tourism.utils.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/route")
public class RouteController {
    @Resource
    private RouteService routeService;

    /**
     * 获取routeMap集合
     * @return
     */
    @RequestMapping("/getRouteMap")
    public String getRouteMap(){
        Map<String, List<Route>> routeMap = routeService.getRouteMap();
        return new Result<Map<String,List<Route>>>().success("查询成功",routeMap);
    }
    @GetMapping("/searchRoutes/{page}")
    public String searchRoutes(Route route,@PathVariable("page")Integer page){
        try {
            /**
             * 参数：关键字，开始时间，最大价格
             */
            PageHelper.startPage(page, 5);
            List<Route> routes = routeService.searchRoutes(route);
          return new Result<List<Route>>().success(new PageInfo(routes));
        }catch (TourismException e){
            return new Result<>().error(e.getMessage());
        }
    }
    @RequestMapping("/getRouteDetails/{rid}")
    public String getRouteDetails(@PathVariable("rid") Integer rid){
        try {
            Map<String, Object> routeDetails = routeService.getRouteDetails(rid);
            return new Result<>().success("查询成功",routeDetails);
        }catch (TourismException e){
            return new Result<>().error(e.getMessage());
        }
    }

    @RequestMapping("/routeList/{page}")
    public String getRouteList(@PathVariable("page") Integer page){
        PageHelper.startPage(page, 5);
        return new Result<List<Route>>().success(new PageInfo(routeService.getAllRoutes()));
    }
    @RequestMapping("/getHotRoutes")
    public String getHotRoutes(){
        return new Result<>().success(routeService.getHotRoutes());
    }
    @RequestMapping("/getNewRoutes")
    public String getNewRoutes(){
        return new Result<>().success(routeService.getNewRoutes());
    }
}
